package com.fcy.teamwork.business.main.homepage.adapter

import android.app.ActionBar.LayoutParams
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.FragmentActivity
import com.fcy.teamwork.R
import com.fcy.teamwork.base.BaseRVAdapter
import com.fcy.teamwork.base.toJson
import com.fcy.teamwork.business.main.homepage.TaskInfoFrag
import com.fcy.teamwork.databinding.ItemTaskBriefBinding
import com.fcy.teamwork.pojo.JobDetail

class TaskMarketListAdapter : BaseRVAdapter<JobDetail, ItemTaskBriefBinding>() {
    override fun getItemViewType(position: Int): Int = 0


    override fun bindView(holder: VH, position: Int, data: JobDetail) {
        (holder.binding as? ItemTaskBriefBinding)?.apply {
            refresh(data)
        }
    }


    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ) = ItemTaskBriefBinding.inflate(inflater, parent, false)
}
public fun ItemTaskBriefBinding.refresh(data: JobDetail) {
    tvTaskInfo.setOnClickListener {
        (root.context as? FragmentActivity)?.supportFragmentManager?.apply {
            beginTransaction().add(TaskInfoFrag::class.java, Bundle().apply {
                //todo 判断是否已经加入该项目
                putBoolean(
                    "canApply", data.job.isResolved != 1
                )
                putString("jobDetail", data.toJson())
            }, "TaskInfoFrag")
                .commitNow()
        }
    }
    fvRequire.removeAllViews()
    data.job.requireSkills.split(",").forEach {
        val layoutParams =
            LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)
                .apply {
                    marginEnd = 20
                    bottomMargin = 20
                }
        val textView = TextView(root.context).apply {
            background = ResourcesCompat.getDrawable(
                root.resources,
                R.drawable.label_style,
                root.context.theme
            )
            gravity = Gravity.CENTER
            setPadding(10, 0, 10, 0)
            textSize = 18F
            text = it
        }
        fvRequire.addView(textView, layoutParams)
    }
    tvTitle.text = data.owner.nikeName
    tvJobName.text = data.job.name
    tvJobType.text = data.projectInfo.type
    tvFaculty.text = data.owner.faculty
    tvTaskTime.text = data.projectInfo.duration ?: "六个月"
}
